package com.cnplan.service.impl;

import com.cnplan.entity.ArcMain;
import com.cnplan.mapper.ArcMainMapper;
import com.cnplan.service.ArcMainService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArcMainServiceImpl implements ArcMainService {

    @Autowired
    private ArcMainMapper arcMainMapper;

    @Override
    public PageInfo findByArcMain(Map map) {
        if(!map.containsKey("pageNo")){
            map.put("pageNo",1);
        }
        if(!map.containsKey("pageSize")){
            map.put("pageSize",10);
        }
        PageHelper.startPage(Integer.parseInt(map.get("pageNo").toString()),
                Integer.parseInt(map.get("pageSize").toString()));
        List<ArcMain> List=arcMainMapper.selectArcMain(sqlquery(map));
        PageInfo pageInfo = new PageInfo(List);
        return pageInfo;
    }

    @Override
    public ArcMain findByArcNum(String arcNum) {
        return arcMainMapper.selectArcNum(arcNum);
    }

    private static String sqlquery(Map map){
        StringBuffer sb=new StringBuffer();
        String featureType=map.containsKey("featureType")?map.get("featureType").toString():null;
        //档号
        String arcnum=map.containsKey("arcNumsel")?map.get("arcNumsel").toString():null;
        //验收合格证号
        String checkNum=map.containsKey("checkNum")?map.get("checkNum").toString():null;
        //项目名称
        String prjName=map.containsKey("prjName")?map.get("prjName").toString():null;
        //工程地址
        String prjAddr=map.containsKey("prjAddr")?map.get("prjAddr").toString():null;
        //许可证号
        String acpNum=map.containsKey("acpNum")?map.get("acpNum").toString():null;
        //建设单位
        String bfName=map.containsKey("bfName")?map.get("bfName").toString():null;
        //建设单位地址
        String bdAdr=map.containsKey("bdAdr")?map.get("bdAdr").toString():null;
        //编制单位
        String planUnits=map.containsKey("planUnits")?map.get("planUnits").toString():null;

        if(featureType!=null && !"".equals(featureType)){
            if(!"zh".equals(featureType)){
                sb.append(" and feature_type = '"+featureType+"'");
            }
        }
        if(arcnum!=null && !"".equals(arcnum)){
            String[] arcNumArray=arcnum.split(" ");
            for (String str:arcNumArray) {
                sb.append(" and arc_num like '%"+str.trim()+"%'");
            }
        }
        if(checkNum!=null && !"".equals(checkNum)){
            String[] checkNumArray=checkNum.split(" ");
            for (String str:checkNumArray) {
                sb.append(" and check_num like '%"+str.trim()+"%'");
            }
        }
        if(prjName!=null && !"".equals(prjName)){
            String[] prjNameArray=prjName.split(" ");
            for (String str:prjNameArray) {
                sb.append(" and prj_name like '%"+str.trim()+"%'");
            }
        }
        if(prjAddr!=null && !"".equals(prjAddr)){
            String[] prjAddrArray=prjAddr.split(" ");
            for (String str:prjAddrArray) {
                sb.append(" and prj_addr like '%"+str.trim()+"%'");
            }
        }
        if(acpNum!=null && !"".equals(acpNum)){
            String[] acpNumArray=acpNum.split(" ");
            for (String str:acpNumArray) {
                sb.append(" and acp_num like '%"+str.trim()+"%'");
            }
        }
        if(bfName!=null && !"".equals(bfName)){
            String[] bfNameArray=bfName.split(" ");
            for (String str:bfNameArray) {
                sb.append(" and bf_name like '%"+str.trim()+"%'");
            }
        }
        if(bdAdr!=null && !"".equals(bdAdr)){
            String[] bdAdrArray=bdAdr.split(" ");
            for (String str:bdAdrArray) {
                sb.append(" and bd_adr like '%"+str.trim()+"%'");
            }
        }
        if(planUnits!=null && !"".equals(planUnits)){
            String[] planUnitsArray=planUnits.split(" ");
            for (String str:planUnitsArray) {
                sb.append(" and plan_units like '%"+str.trim()+"%'");
            }
        }
        return sb.toString();
    }
}
